package com.example.driftwindow;




import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;

import android.widget.EditText;
import android.widget.ImageButton;

import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends BaseActivity {
	
	String strSelected = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

		setContentView(R.layout.daohang);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		super.addButtons();
		Spinner spinner = (Spinner) findViewById(R.id.spinner1);
		
		final String[] stringArray = getResources().getStringArray(
				R.array.stringArray);
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, stringArray);
		spinner.setAdapter(adapter);

		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				int index = arg0.getSelectedItemPosition(); // ����������pos
				Toast.makeText(getBaseContext(),
						"SELECT:" + stringArray[index], Toast.LENGTH_SHORT)
						.show();
				strSelected = stringArray[index];
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	

		View.OnClickListener searchclick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub

				EditText EditContent = (EditText) findViewById(R.id.editcontent);
				String strEditContent = EditContent.getText().toString();

				Uri uri = Uri.parse("");

				if (strSelected
						.equals(getResources().getString(R.string.baidu))) {
					uri = Uri.parse("http://www.baidu.com/s?wd="
							+ strEditContent);
				}
				if (strSelected.equals("360")) {
					uri = Uri.parse("http://www.so.com/s?&q=" + strEditContent);
				}
				if (strSelected.equals(getResources()
						.getString(R.string.google))) {
					uri = Uri.parse("http://www.google.com.hk/search?&q="
							+ strEditContent);
				}
				if (strSelected
						.equals(getResources().getString(R.string.sogou))) {
					uri = Uri.parse("http://www.sogou.com/web?query="
							+ strEditContent);
				}
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		};
		
		View.OnClickListener dengluhandler = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				EditText EditContent = (EditText) findViewById(R.id.editcontent);
				String strEditContent = EditContent.getText().toString();

				Uri uri = Uri.parse("");

				int bytesLength = strEditContent.getBytes().length;
				int sLength = strEditContent.length();
				int hasNum = bytesLength - sLength;
				if (hasNum == 0) {
					
					uri = Uri
							.parse("http://www.dengluyi.com/index2.php?type=url&client=mobile&fwmc="
									+ strEditContent);
				} else {
					
					uri = Uri
							.parse("http://www.dengluyi.com/index2.php?type=web&client=mobile&fwmc="
									+ strEditContent);
				}
				Intent it = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(it);
			}
		};
		final ImageButton btn_Search = (ImageButton) findViewById(R.id.searchbutton);
		btn_Search.setOnClickListener(searchclick);

		final ImageButton btn_Login = (ImageButton) findViewById(R.id.denglubutton);
		btn_Login.setOnClickListener(dengluhandler);
	}
	
	


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
}
